package com.smile.service.handler;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSONObject;
import com.smile.api.InspectionTaskApi;
import com.smile.commons.page.R;
import com.smile.mapper.PlanarAirlineMapper;
import com.smile.model.request.GetInspectionTaskByFlightIdRequest;
import com.smile.model.response.GetInspectionTaskByFlightIdResponse;
import com.smile.service.config.mqtt.handler.AbstractFlighttaskResourceGet;
import com.smile.service.entity.requests.BaseRequestsEntity;
import com.smile.service.entity.requests.FlighttaskResourceGet;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class DefaultFlighttaskResourceGet extends AbstractFlighttaskResourceGet {


    @Autowired
    private InspectionTaskApi inspectionTaskApi;

    @Autowired
    private PlanarAirlineMapper planarAirlineMapper;


    @Override
    public Boolean process(String data, String tid, String bid, String gateway) throws MqttException {
        String flightId = com.alibaba.fastjson.JSONObject.parseObject(data).getString("flight_id");
        GetInspectionTaskByFlightIdRequest getInspectionTaskByFlightIdRequest = new GetInspectionTaskByFlightIdRequest();
        getInspectionTaskByFlightIdRequest.setFlightId(flightId);
        R<GetInspectionTaskByFlightIdResponse> inspectionTaskByFlightId = inspectionTaskApi.getInspectionTaskByFlightId(getInspectionTaskByFlightIdRequest);
        if (ObjectUtil.isNotNull(inspectionTaskByFlightId) && ObjectUtil.isNotNull(inspectionTaskByFlightId.getData())) {
        BaseRequestsEntity<FlighttaskResourceGet> baseRequestsEntity = new BaseRequestsEntity<>();
        baseRequestsEntity.setBid(bid);
        baseRequestsEntity.setTid(tid);
//        baseRequestsEntity.setBid(UUID.randomUUID().toString());
//        baseRequestsEntity.setTid(UUID.randomUUID().toString());
        FlighttaskResourceGet flighttaskResourceGet = new FlighttaskResourceGet();
        flighttaskResourceGet.setResult(0);
        FlighttaskResourceGet.Output output = new FlighttaskResourceGet.Output();
        FlighttaskResourceGet.Output.File file = new FlighttaskResourceGet.Output.File();
//            file.setFingerprint(inspectionTaskByFlightId.getData().getEtag());
//            file.setUrl(inspectionTaskByFlightId.getData().getUrl());


        //http://www.smile-emo.com/kml/file/kmz/8456df4b9512494ea8bfc553be6720f9.kmz
            file.setUrl(inspectionTaskByFlightId.getData().getUrl());
            file.setFingerprint(inspectionTaskByFlightId.getData().getEtag());

//        FeisuanPlanarAirline feisuanPlanarAirline = planarAirlineMapper.selectById(8);
//
//        file.setUrl(feisuanPlanarAirline.getAirlineUrl());
//        file.setFingerprint(feisuanPlanarAirline.getETag());

        output.setFile(file);
        flighttaskResourceGet.setOutput(output);
        baseRequestsEntity.setData(flighttaskResourceGet);
        baseRequestsEntity.setTimestamp(System.currentTimeMillis());
        baseRequestsEntity.setMethod("flighttask_resource_get");

        log.info("DefaultFlighttaskResourceGet {}", JSONObject.toJSONString(baseRequestsEntity));

        MqttMessage message = new MqttMessage();
        message.setQos(0);
        message.setRetained(true);
        message.setPayload(JSONObject.toJSONString(baseRequestsEntity).getBytes());
        MqttClient mqttClient = new MqttClient("tcp://120.211.119.131:1883", "feisuan-mqtt-producer-client", new MemoryPersistence());
        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setUserName("feisuan");
        connOpts.setPassword("feisuan123456".toCharArray());
        mqttClient.connect(connOpts);

        mqttClient.publish("thing/product/" + gateway + "/requests_reply", message);
//        mqttCli
        return Boolean.TRUE;
        } else {
            return Boolean.FALSE;
        }


    }
}
